package com.db4o.internal.odbgen.fileutils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Recursively searches for files and returns them in a list.
 * @author liviug
 *
 */
public class FileLister extends DirIterator {
    private File _baseDir;
    private List<File> _files;
    private String _regexFilter;

    /**
     * Constructs the object.
     * @param baseDir The base directory files are enumerated for.
     * @param regexFilter A regex string to filter file names. Those files that don't match the
     * regex will be ignored.
     */
    public FileLister(File baseDir, String regexFilter){
        this.setEnterSubdirectories(true);
        this._baseDir = baseDir;
        this._regexFilter = regexFilter;
    }

    /**
     * Called by the base class for each enumerated file.
     */
    @Override
    protected void process(File f) throws IOException {
        if(f.isFile()){
            if(f.getName().matches(this._regexFilter)){
                this._files.add(f);
            }
        }
    }
    
    /**
     * Call this in order to generate the list of files.
     * After this call, this.getFileList() will return the list.
     */
    public void start() throws IOException{
        this._files = new ArrayList<File>();
        super.start(this._baseDir.getCanonicalPath());
    }
    
    /**
     * Returns the file list after this.start() has been completed.
     */
    public List<File> getFileList(){
        return this._files;
    }
}



